﻿Public Class Upgrade

    Private Sub Upgrade_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.Cursor = Cursors.WaitCursor


        Dim DownloadURL As String = "http://www.formition.com/Password Safe.exe"
        Dim DownloadFilename As String = "Password Safe.exe.tmp"

        Try

            'version check, no need, just get latest anyhow...
            Me.ProgressBar1.Value = 15

            'begin download
            Me.ProgressBar1.Value = 30

            Dim UT As New UpgradeTools
            If UT.Download(DownloadURL, DownloadFilename) Then

                'begin download
                Me.ProgressBar1.Value = 45

                Dim source As String = Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\Formition", "ApplicationRunningPath", "") & "\" & DownloadFilename
                Dim destination As String = Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\Formition", "ApplicationRunningPath", "") & "\Password Safe.exe"
                FileCopy(source, destination)

                'begin download
                Me.ProgressBar1.Value = 100

                Me.Cursor = Cursors.Default

                Call MsgBox("Finished Upgrading, the new version will start now", MsgBoxStyle.Information, "Formition Password Safe Upgrade")
                Dim rc As String
                rc = Shell(Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\Formition", "ApplicationRunningPath", "") & "\Password Safe.exe", vbNormalFocus)
                Close()
            End If

            'if we get here, I don't know what happened.
        Catch ex As Exception
            Me.Cursor = Cursors.Default

            Call MsgBox("An error occurred. Error Message: " & ex.Message, MsgBoxStyle.Exclamation)
        End Try

    End Sub
End Class